package com.open.capacity.form.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.form.model.FormDbField;
import com.open.capacity.form.service.FormDbFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 数据库常用字段管理
 */
@RestController
@Api(tags = "数据库常用字段模块api")
public class FormDbFieldController {


    @Autowired
    private FormDbFieldService dbFieldService;


    /**
     * 查询数据库常用字段
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('dbField:get/dbFields')")
    @ApiOperation(value = "查询数据库常用字段")
    @GetMapping("/dbFields")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public Result<List<FormDbField>> findDbFields(@RequestParam Map<String, Object> params) {
        List<FormDbField> list = dbFieldService.findDbFields(params);
        return Result.succeed(list);
    }


    /**
     * 数据库常用字段新增或者更新
     *
     * @param sysDbField
     * @return
     */
    @PreAuthorize("hasAnyAuthority('dbField:post/dbFields','dbField:put/dbFields')")
    @ApiOperation(value = "保存数据库常用字段")
    @PostMapping("/dbFields/saveOrUpdate")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public Result saveOrUpdate(@RequestBody FormDbField sysDbField, String keyValue) {
        return dbFieldService.saveOrUpdate(sysDbField, keyValue);
    }


    /**
     * 删除数据库常用字段
     * delete /dbFields/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('dbField:delete/dbFields/{id}')")
    @ApiOperation(value = "删除数据库常用字段")
    @DeleteMapping("/dbFields/{id}")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            dbFieldService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("操作失败");
        }
    }

}
